//
//  NewsCell.m
//  NeteaseNewsLY
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NewsCell.h"

@interface NewsCell ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>

@end

@implementation NewsCell
- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [_threePicArr release];
    [_tView release];
    [_timer release];
    [_lunbodataArr release];
    [super dealloc];
    
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 创建tableView
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT * 40, WIDTH * 375, HEIGHT * 667) style:UITableViewStylePlain];
        [self headerData];
        [self.contentView addSubview:_tableView];
        [_tableView release];
    }
    return self;
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    // 创建view放轮播图
    self.tView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH *  375, HEIGHT * 211)];
    [_tView release];
    [self lunbo];
    
    // 头部轮播图
    self.tableView.tableHeaderView = _tView;
    
    [_tableView registerClass:[NewTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [_tableView registerClass:[ThreePicCell class] forCellReuseIdentifier:@"threePiccell"];
    
    // 下拉刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self headerData];
            [self.tableView.mj_header endRefreshing];
        });
    }];
    self.tableView.mj_header = header;
    // 上拉加载
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self footerData];
        [self.tableView.mj_footer endRefreshing];
    }];
    self.tableView.mj_footer = footer;
}
// 下拉刷新
- (void)headerData
{
    NSString *str = @"http://api.iclient.ifeng.com/ClientNews?id=SYLB10,SYDT10,SYRECOMMEND&page=1&newShowType=1&province=%E8%BE%BD%E5%AE%81%E7%9C%81&city=%E5%A4%A7%E8%BF%9E%E5%B8%82&district=%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA&gv=4.5.1&av=4.5.1&uid=357507050009649&deviceid=357507050009649&proid=ifengnews&os=android_18&df=androidphone&vt=5&screen=1080x1920&publishid=2006";
    [AFHTTPHandler GETWithURL:str completion:^(id result) {
        NSArray *arr = result[0][@"item"];
        self.dataArr = [NewModel modelHandleWithArray:arr];
        self.threePicArr = result[0][@"slides"];
        //        self.SecondDataArr = [NewModel modelHandleWithArray:arr2];
        // 遍历第二种cell数据结果 将value值放入同一个数组
        for (NSDictionary *dic in _threePicArr) {
            NewModel *m = [[NewModel alloc] init];
            [m setValuesForKeysWithDictionary:dic];
            [self.dataArr addObject:m];
            [m release];
        }
        // 打乱同数组中两种cell数据的顺序
        for (int i = 0; i < _dataArr.count; i++) {
            int m = arc4random() %_dataArr.count;
            [_dataArr exchangeObjectAtIndex:i withObjectAtIndex:m];
        }
        
        [self.tableView reloadData];
    }];
    [_tableView reloadData];
}
// 上拉加载
- (void)footerData
{
    static NSInteger t = 2;
    
    NSString *str = @"http://api.iclient.ifeng.com/ClientNews?id=SYLB10,SYDT10,SYRECOMMEND&page=1&newShowType=1&province=%E8%BE%BD%E5%AE%81%E7%9C%81&city=%E5%A4%A7%E8%BF%9E%E5%B8%82&district=%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA&gv=4.5.1&av=4.5.1&uid=357507050009649&deviceid=357507050009649&proid=ifengnews&os=android_18&df=androidphone&vt=5&screen=1080x1920&publishid=2006";
    NSString *urlStr = [str stringByReplacingOccurrencesOfString:@"page=1" withString:[NSString stringWithFormat:@"page=%ld", t]];
    [AFHTTPHandler GETWithURL:urlStr completion:^(id result) {
        NSArray *tempArr = [NewModel modelHandleWithArray:result[0][@"item"]];
        for (NSInteger i = 0; i < tempArr.count; i++) {
            [_dataArr addObject:tempArr[i]];
        }
        [_tableView reloadData];
    }];
    t += 1;
    
}
//- (void)lunbo
//{
//    // UIView作为tableView头部
//    self.tView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 375,HEIGHT * 211)];
//    [_tView release];
//    _tView.backgroundColor = [UIColor cyanColor];
//    // 创建scrollView
//    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 211)];
//    [_tView addSubview:_scrollView];
//    [_scrollView release];
//    // 轮播图数据
//    [self lunbodata];
//    // 偏移量
//    self.scrollView.contentSize = CGSizeMake(self.contentView.frame.size.width * _lunbodataArr.count , 0);
//    // 整页移动
//    self.scrollView.pagingEnabled = YES;
//    self.scrollView.showsHorizontalScrollIndicator = NO;
//    
//    for (NSInteger i = 0; i < _lunbodataArr.count; i++) {
//        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(i * WIDTH * 375, 0, WIDTH * 375,HEIGHT * 211)];
//        [imgView sd_setImageWithURL:[NSURL URLWithString:[_lunbodataArr[i] thumbnail]]];
//        [_scrollView addSubview:imgView];
//        [imgView release];
//        // 图片上的文字
//        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 10, HEIGHT * 180, WIDTH * 300, HEIGHT * 25)];
//        self.titleLabel.textColor = [UIColor whiteColor];
//        self.titleLabel.text = [_lunbodataArr[i] title];
//        [imgView addSubview:_titleLabel];
//        [_titleLabel release];
//        // 跳到第几张
//        UILabel *labelNum = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 315, HEIGHT * 180, WIDTH * 50,HEIGHT * 25)];
//        labelNum.textColor = [UIColor whiteColor];
//        //        labelNum.text = [ [NSString stringWithFormat:@"%ld", i + 1 ] stringByAppendingFormat:@"/%ld", _lunbodataArr.count - 1];
//        labelNum.text = [NSString stringWithFormat:@"%ld/3", i + 1];
//        if (i == 3) {
//            labelNum.text = @"1/3";
//        }
//        [imgView addSubview:labelNum];
//        [labelNum release];
//    }
//    // 定时器
////    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(roll) userInfo:nil repeats:YES];
//    
////    self.tableView.tableHeaderView = _tView;
//    
//    // 开启用户交互
//    _scrollView.userInteractionEnabled = YES;
//    // scrollView创建轻拍
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//    // 轻拍次数
//    tap.numberOfTapsRequired = 1;
//    // 手指个数
//    tap.numberOfTouchesRequired = 1;
//    // 把手势添加给视图
//    [self.scrollView addGestureRecognizer:tap];
//    [tap release];
//    
//}
- (void)lunbo
{
    NSString *str = @"http://api.iclient.ifeng.com/ClientNews?id=SYLB10,SYDT10,SYRECOMMEND&page=1&newShowType=1&province=%E8%BE%BD%E5%AE%81%E7%9C%81&city=%E5%A4%A7%E8%BF%9E%E5%B8%82&district=%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA&gv=4.5.1&av=4.5.1&uid=357507050009649&deviceid=357507050009649&proid=ifengnews&os=android_18&df=androidphone&vt=5&screen=1080x1920&publishid=2006";
    [AFHTTPHandler GETWithURL:str completion:^(id result) {
        self.lunbodataArr = result[1][@"item"];
        NSMutableArray *photoArr = [NSMutableArray array];
        
        for (NSDictionary *dic in _lunbodataArr) {
            [photoArr addObject:dic[@"thumbnail"]];
            
        }        

        // 调用轮播图
        CarouselPicture *lunbotu = [[CarouselPicture alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 211) timeInterval:2 imageUrlArr:photoArr urlArr:nil];
        [self.tableView addSubview:lunbotu];
        [lunbotu release];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewModel *m = _dataArr[indexPath.row];
    if ([m.type isEqualToString:@"slide"]) {
        ThreePicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"threePiccell" forIndexPath:indexPath] ;
        [cell.oneImg sd_setImageWithURL:[NSURL URLWithString:m.images[0]] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        [cell.twoImg sd_setImageWithURL:[NSURL URLWithString:m.images[1]] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        [cell.threeImg sd_setImageWithURL:[NSURL URLWithString:m.images[2]] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        cell.title.text = m.title;
        cell.updateTime.text = m.updateTime;
        cell.updateTime.text = [m.updateTime substringWithRange:NSMakeRange(11, 5)];
        cell.commentsall.text = [m.commentsall stringByAppendingString:@"跟帖"];
        return cell;
    }
    else{
        NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]  ;
        [cell.picture sd_setImageWithURL:[NSURL URLWithString:m.thumbnail] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        cell.title.text = m.title ;
        cell.updateTime.text = [m.updateTime substringWithRange:NSMakeRange(11, 5)];
        cell.commentsall.text = [m.commentsall stringByAppendingString:@"跟帖"];
        return cell;
        
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewModel *m = _dataArr[indexPath.row];
    if ([m.type isEqualToString:@"slide"]) {
        return HEIGHT * 150;
    }else{
        return HEIGHT * 110;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewModel *m = _dataArr[indexPath.row];
    [self.delegate newsValue:m.commentsUrl value1:m.title];
    
}

@end
